package com.aso.common.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import com.itsdf07.alog.ALog;


/**
 * App相关信息获取工具类
 * Created by itsdf07 on 2017/5/9 14:31.
 * E-Mail: 923255742@qq.com
 * GitHub: https://github.com/itsdf07
 */

public class AppUtils {

    /**
     * 获取app的 versionCode
     *
     * @param context
     * @return 如 1
     */
    public static int getVersionCode(Context context) {
        PackageManager packageManager = context.getPackageManager();
        PackageInfo packageInfo;
        int versionCode = -1;
        try {
            packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            versionCode = packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    /**
     * 获取app的 versionName
     *
     * @param context
     * @return 如 1.0.0
     */
    public static String getVersionName(Context context) {
        PackageManager packageManager = context.getPackageManager();
        PackageInfo packageInfo;
        String versionName = "";
        try {
            packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            versionName = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

    /**
     * 获取渠道代号
     * 获取不到渠道号的默认为Rchat渠道
     *
     * @return
     */
    public static String getRchatChannelCode() {
        String channelName = getAppMetaData();
        String channelCode = "1";
        if (TextUtils.isEmpty(channelName)
                || "rchat".equals(channelName)) {
            channelCode = "1";
        } else if ("baidu".equals(channelName)) {
            channelCode = "2";
        } else if ("yingyongbao".equals(channelName)) {
            channelCode = "3";
        } else if ("oppo".equals(channelName)) {
            channelCode = "4";
        } else if ("r360".equals(channelName)) {
            channelCode = "5";
        } else if ("xiaomi".equals(channelName)) {
            channelCode = "6";
        } else if ("huawei".equals(channelName)) {
            channelCode = "7";
        } else if ("vivo".equals(channelName)) {
            channelCode = "8";
        } else if ("ali".equals(channelName)) {
            channelCode = "9";
        } else {
            channelCode = "1";
        }
        ALog.dTag("rchatchannel", "channelName = %s,channelCode = %s", channelName, channelCode);
        return channelCode;
    }

    /**
     * 获取application中指定的meta-data。本例中，调用方法时key就是UMENG_CHANNEL
     *
     * @return 如果没有获取成功(没有对应值，或者异常)，则返回值为空
     */
    private static String getAppMetaData() {
        if (Utils.getApplication() == null) {
            return null;
        }
        String resultData = null;
        try {
            PackageManager packageManager = Utils.getApplication().getPackageManager();
            if (packageManager != null) {
                ApplicationInfo applicationInfo = packageManager.getApplicationInfo(Utils.getApplication().getPackageName(), PackageManager.GET_META_DATA);
                if (applicationInfo != null) {
                    if (applicationInfo.metaData != null) {
                        resultData = applicationInfo.metaData.getString("RCHAT_CHANNEL");
                    }
                }

            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return resultData;
    }
//    使用getAppMetaData（context,"getAppMetaData"）
}
